package com.tencent.mid.a;

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.util.Log;
import com.tencent.mid.api.MidCallback;
import com.tencent.mid.api.MidEntity;
import com.tencent.mid.b.a;
import com.tencent.mid.util.Util;
import com.tencent.mid.util.i;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;

public class d
{
  private static String b = "iikVs3FGzEQ23RaD1JlHsSWSI5Z26m2hX3gO51mH3ag=";
  private static d c = null;
  private static Context d = null;
  Handler a = null;

  private d(Context paramContext)
  {
    try
    {
      HandlerThread localHandlerThread = new HandlerThread("HttpManager");
      localHandlerThread.start();
      this.a = new Handler(localHandlerThread.getLooper());
      d = paramContext.getApplicationContext();
      return;
    }
    catch (Throwable paramContext)
    {
      Util.logWarn(paramContext);
    }
  }

  static Context a()
  {
    return d;
  }

  public static d a(Context paramContext)
  {
    monitorenter;
    try
    {
      if (c == null)
        c = new d(paramContext);
      paramContext = c;
      return paramContext;
    }
    finally
    {
      monitorexit;
    }
  }

  private String a(f paramf, MidCallback paramMidCallback)
  {
    MidEntity localMidEntity;
    int k = -1;
    int j = 1;
    boolean bool = false;
    int i = paramf.a();
    paramf = paramf.b();
    if (i == 200)
    {
      if (!(Util.isStringValid(paramf)))
        break label337;
      JSONObject localJSONObject = new JSONObject(paramf);
      paramf = localJSONObject.optString("mid", null);
      if (!(Util.isMidValid(paramf)))
        break label331;
      localMidEntity = new MidEntity();
      localMidEntity.setMid(paramf);
      localMidEntity.setMac(Util.getWifiMacAddress(d));
      localMidEntity.setImei(Util.getImei(d));
      if (!(localJSONObject.isNull("ts")))
      {
        long l = localJSONObject.optLong("ts");
        if (l > 0L)
          localMidEntity.setTimestamps(l);
        Util.logInfo("new mid midEntity:" + localMidEntity.toString());
        paramMidCallback.onSuccess(localMidEntity.toString());
        if (localJSONObject.optInt("force", 0) == 1)
          bool = true;
        localMidEntity.setForceWrite(bool);
        com.tencent.mid.b.g.a(d).a(localMidEntity);
        if (localJSONObject.isNull(a.c))
          break label326;
        i = localJSONObject.getInt(a.c);
        if (localJSONObject.isNull(a.d))
          break label323;
        k = localJSONObject.getInt(a.d);
      }
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            com.tencent.mid.b.g.a(d).a(i, k);
            if (j == 0)
              paramMidCallback.onSuccess(com.tencent.mid.b.g.a(d).a());
            return paramf;
            localMidEntity.setTimestamps(System.currentTimeMillis());
          }
          paramf = "Server response error code:" + i + ", error:" + paramf;
          Util.logInfo(paramf);
          paramMidCallback.onFail(i, paramf);
          return "0";
          label323: continue;
          label326: i = -1;
        }
        label331: j = 0;
      }
      label337: j = 0;
      paramf = "0";
      i = -1;
    }
  }

  private String b()
  {
    return Util.decode(b);
  }

  private void b(g paramg, MidCallback paramMidCallback)
  {
    while (true)
    {
      int i;
      Object localObject1;
      try
      {
        localObject1 = Util.getHttpUrl();
        Object localObject2 = b.a((String)localObject1);
        if (((f)localObject2).a() != 200)
        {
          str = "response code invalid:" + ((f)localObject2).a();
          Util.logInfo(str);
          paramMidCallback.onFail(((f)localObject2).a(), str);
          return;
        }
        i = 0;
        String str = null;
        localObject2 = new JSONObject(((f)localObject2).b());
        if (((JSONObject)localObject2).isNull("rand"))
          break label474;
        i = ((JSONObject)localObject2).getInt("rand");
        str = i.a(Util.getHMAC(b(), String.valueOf(i)));
        break label474:
        Util.logInfo("hmac == null");
        return;
        localObject2 = new HashMap();
        ((HashMap)localObject2).put("k", str);
        ((HashMap)localObject2).put("s", String.valueOf(i));
        str = ((String)localObject1) + b.a((Map)localObject2);
        localObject1 = b.a(str);
        if (((f)localObject1).a() != 200)
        {
          Util.logInfo("hmac invalid.");
          paramMidCallback.onFail(((f)localObject1).a(), "hmac invalid.");
          return;
        }
        localObject1 = new JSONObject();
        paramg.a((JSONObject)localObject1);
        Util.jsonPut((JSONObject)localObject1, "rip", Util.getRemoteUrlIp(Util.getHttpUrl()));
        str = a(b.a(str, "[" + ((JSONObject)localObject1).toString() + "]"), paramMidCallback);
        if (Util.isMidValid(((JSONObject)localObject1).optString("mid")))
          break label434;
        label434: throw new Exception("get Mid failed, something wrong");
      }
      catch (Throwable localThrowable)
      {
        Log.w("MID", "request MID from MID server failed, try to connect MTA server", localThrowable);
      }
      finally
      {
        try
        {
          localObject1 = new JSONObject();
          paramg.a((JSONObject)localObject1);
          ((JSONObject)localObject1).put("ky", "A81AG9CN6AE8");
          Util.jsonPut((JSONObject)localObject1, "rip", Util.getRemoteUrlIp("http://pingma.qq.com:80"));
          a(b.a("http://pingma.qq.com:80/mstat/report/", "[" + ((JSONObject)localObject1).toString() + "]"), paramMidCallback);
          b.b();
          return;
          b.b();
          return;
        }
        catch (Throwable paramg)
        {
          while (true)
          {
            paramMidCallback.onFail(-10030, localThrowable.toString());
            Log.e("MID", "request MID  failed", localThrowable);
          }
          paramg = finally;
          b.b();
          throw paramg;
        }
      }
      label474: if (localThrowable == null)
        continue;
      if (i != 0)
        continue;
    }
  }

  void a(g paramg, MidCallback paramMidCallback)
  {
    if ((paramg == null) || (this.a == null) || (paramMidCallback == null))
    {
      if (paramMidCallback != null)
        paramMidCallback.onFail(-10000, "packet == null || handler == null");
      return;
    }
    if (Thread.currentThread().getId() == this.a.getLooper().getThread().getId())
    {
      b(paramg, paramMidCallback);
      return;
    }
    this.a.post(new e(this, paramg, paramMidCallback));
  }
}